package com.bsoft.gol.hbs.dao;

import org.atag.dao.action.QoHibernateStatelessResultAction;
import org.atag.dao.model.SimplePageInfo;
import ctd.persistence.annotation.DAOMethod;
import ctd.persistence.annotation.DAOParam;
import ctd.persistence.support.hibernate.HibernateSupportDelegateDAO;
import ctd.persistence.support.hibernate.template.HibernateSessionTemplate;
import hcn.base.announcement.DocAnnouncement;
import hcn.util.CommonUtils;
import pcn.bean.announcement.QueryAnnouncementListQo;


/**
 * @Author: lanyu
 * @Description: 院内公告
 * @Date: create in 2022/8/4 16:29
 */
public abstract class AnnouncementDAO extends HibernateSupportDelegateDAO<DocAnnouncement> {
    public AnnouncementDAO() {
        this.setKeyField("announcementId");
    }

    @DAOMethod(sql = "update DocAnnouncement set announcementStatus=:announcementStatus  where announcementId=:announcementId  ")
    public abstract void updateDocAnnouncement(@DAOParam("announcementId") String announcementId, @DAOParam("announcementStatus") String announcementStatus);


    public SimplePageInfo<DocAnnouncement> queryAnnouncementList(QueryAnnouncementListQo qo){
        QoHibernateStatelessResultAction<DocAnnouncement, QueryAnnouncementListQo> action = new QoHibernateStatelessResultAction(true, qo, DocAnnouncement.class);
        action.setSql(" SELECT {{" +
                " a.announcement_id AS announcementId, " +
                " a.org_id as orgId, " +
                " a.org_name as orgName, " +
                " a.publish_dept_name as publishDeptName, " +
                " a.title, " +
                " a.announcement_type as announcementType, " +
                " a.announcement_status as announcementStatus, " +
                " a.content, " +
                " a.create_time as publishTime, " +
                " a.create_time as createTime, " +
                " a.attach_file_id AS attachFileId }}" +
                "FROM " +
                " doc_announcement a ");
        action.addSql(" where 1=1  and delete_flag = '0' ");
        if (CommonUtils.isNotEmpty(qo.getOrgIdList()) && CommonUtils.isNotEmpty(qo.getTenantIdList())) {
            action.addCondition(" and (a.org_id in(:orgIdList) or a.tenant_id in(:tenantIdList)) ");
        }else{
            action.addCondition(" and a.org_id in(:orgIdList) ");
            action.addCondition(" and a.tenant_id in(:tenantIdList) ");
        }
        action.addCondition(" and a.org_id =:orgId ");
        action.addCondition(" and a.announcement_type =:announcementType ");
        action.addCondition(" and a.announcement_status =:announcementStatus ");
        action.addCondition(" and a.title like :%title% ");
        action.addSql("  ORDER BY   publish_time  DESC  ");
        HibernateSessionTemplate.instance().execute(action);
        SimplePageInfo<DocAnnouncement> pageInfo = action.getSimpleResultPage();
        return pageInfo;
    }
}